package com.zuikaku.tool;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ZuikakuTool {

    private static SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static List<File> getExcelFileListRecursive(File rootFolder){
        List<File> resultFileList = new ArrayList<File>();
        File[] sonFiles = rootFolder.listFiles();
        if(sonFiles!=null) {
            for (File temp : sonFiles
            ) {
                if (temp.isFile()) {
                    if(!temp.getName().startsWith(".")&&!temp.getName().startsWith("~")&&temp.getName().endsWith("xlsx"))
                        resultFileList.add(temp);

                } else {
                    List<File> excelFileList = getExcelFileListRecursive(temp);
                    resultFileList.addAll(excelFileList);
                }

            }
        }
        return resultFileList;

    }


    public static boolean fixExcel(File originFile,File targetFile){
        try {
            XSSFWorkbook wb = new XSSFWorkbook(originFile);
            String preFolderStr = targetFile.getPath().substring(0,targetFile.getPath().lastIndexOf("\\"));
            File preFolder = new File(preFolderStr);
            boolean isok = preFolder.mkdirs();
            wb.write(new FileOutputStream(targetFile));
            return true;
        } catch (IOException | InvalidFormatException e) {
            e.printStackTrace();
            return false;
        }

    }

    public static String formatDate(Date date){
        return sdf.format(date);

    }

}
